/**
 * 监听对象得具体KEY
 */

import { isRef, Ref } from "vue";

const useWatchPropeyty = <T extends object | Ref<T>>(
  targetObj: T,
  effect: (key: string | symbol) => void,
  keys: any[] = []
) => {
  if (isRef(targetObj)) {
    return targetObj;
  }

  const proxy = new Proxy(targetObj, {
    get(target, key, receiver) {
      return Reflect.get(target, key, receiver);
    },
    set(target, key, value, receiver) {
      Reflect.set(target, key, value, receiver);
      if (keys?.length == 0 || keys.includes(key)) {
        effect(key);
      }
      return true;
    },
  });

  return proxy;
};

export default useWatchPropeyty;
